import 'dart:io';

import 'package:flutter/cupertino.dart';

import '../top_foundation.dart';

ImageProvider loadImageProvider(
    {String? url, String? path, String? placeholder, double scale = 1.0}) {
  if (path != null) {
    return FileImage(File(path));
  }
  if (url != null) {
    if (url.startsWith('http')) {
      return NetworkImage(url);
    } else {
      return AssetImage(
        url,
      );
    }
  }
  return AssetImage(
    placeholder ?? TopConfig.imgPlaceholder,
  );
}

Widget loadPathImage(
    {String? url,
    String? placeholder,
    double? width,
    double? height,
    Color? color,
    int borderradius = 0,
    BoxFit fit = BoxFit.cover}) {
  if (url == null || url.isEmpty) {
    return Image.asset(
      placeholder ?? TopConfig.imgPlaceholder,
      width: width,
      height: height,
      fit: BoxFit.fill,
    );
  }
  if (url.startsWith('http')) {
    return ClipRRect(
      borderRadius: BorderRadius.all(Radius.circular(borderradius.r)),
      child: Image.network(
        url,
        width: width,
        height: height,
        fit: fit,
        // progressIndicatorBuilder: (context, url, progress) {
        //   return CupertinoActivityIndicator(
        //     radius: 15.r,
        //   );
        // },
        // errorWidget: (context, str, obj) {
        //   return loadImage(url: placeholder);
        // },
      ),
    );
  } else {
    return Image.file(
      File(url),
      width: width,
      height: height,
      color: color,
      fit: fit,
    );
  }
}

Widget loadImage(
    {String? url,
    String? placeholder,
    double? width,
    double? height,
    Color? color,
    double borderradius = 0,
    BoxFit fit = BoxFit.cover}) {
  if (url == null || url.isEmpty) {
    return Image.asset(
      placeholder ?? TopConfig.imgPlaceholder,
      width: width,
      height: height,
      fit: BoxFit.fill,
    );
  }
  if (url.startsWith('http')) {
    return ClipRRect(
      borderRadius: BorderRadius.all(Radius.circular(borderradius.r)),
      child: Image.network(
        url,
        width: width,
        height: height,
        fit: fit,
        loadingBuilder: (context, url, progress) {
          if (progress?.cumulativeBytesLoaded == progress?.expectedTotalBytes) {
            return url;
          }
          return CupertinoActivityIndicator(
            radius: 15.r,
          );
        },
        errorBuilder: (context, str, obj) {
          return loadImage(url: placeholder, width: width, height: height);
        },
      ),
    );
  } else {
    return Image.asset(
      url,
      width: width,
      height: height,
      color: color,
      fit: fit,
    );
  }
}

Widget loadAvater({
  String? url,
  String? placeholder,
  required double size,
  BoxFit fit = BoxFit.cover,
  bool isCircle = true,
}) {
  Widget image() {
    return loadImage(
        url: url,
        placeholder: placeholder ?? TopConfig.imgDefaultAvater,
        width: size,
        height: size,
        fit: fit);
  }

  return isCircle
      ? ClipOval(
          child: image(),
        )
      : image();
}
